package com.open.capacity.finance.task;

import com.open.capacity.nk.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @desc 同步内控数据库
 */
@Component
@Slf4j
public class SyncNKdataTask implements ScheduledOfTask {

    @Autowired
    private TaskExecutor taskExecutor;

    @Autowired
    private AaDepartmentService aaDepartmentService;
    @Autowired
    private AaPersonService aaPersonService;

    @Autowired
    private NkApprovalpaymentreceiptexpensesitemService approvalpaymentreceiptexpensesitemService;
    @Autowired
    private NkApprovalpaymentreceiptquotaService approvalpaymentreceiptquotaService;
    @Autowired
    private NkApprovalpaymentreceiptService approvalpaymentreceiptService;
    @Autowired
    private NkApprovalpaymentreceiptsettlementService approvalpaymentreceiptsettlementService;
    @Autowired
    private NkBudgetquotaService budgetquotaService;
    @Autowired
    private NkContractpaymentreceiptexpensesitemService contractpaymentreceiptexpensesitemService;
    @Autowired
    private NkContractpaymentreceiptquotaService contractpaymentreceiptquotaService;
    @Autowired
    private NkContractpaymentreceiptService contractpaymentreceiptService;
    @Autowired
    private NkContractpaymentreceiptsettlementService contractpaymentreceiptsettlementService;
    @Autowired
    private NkDirectpaymentreceiptexpensesitemService directpaymentreceiptexpensesitemService;
    @Autowired
    private NkDirectpaymentreceiptquotaService directpaymentreceiptquotaService;
    @Autowired
    private NkDirectpaymentreceiptService directpaymentreceiptService;
    @Autowired
    private NkDirectpaymentreceiptsettlementService directpaymentreceiptsettlementService;
    @Autowired
    private NkPrepaidpaymentreceiptexpensesitemService prepaidpaymentreceiptexpensesitemService;
    @Autowired
    private NkPrepaidpaymentreceiptquotaService prepaidpaymentreceiptquotaService;
    @Autowired
    private NkPrepaidpaymentreceiptService prepaidpaymentreceiptService;
    @Autowired
    private NkPrepaidpaymentreceiptsettlementService prepaidpaymentreceiptsettlementService;
    @Autowired
    private NkProjectService projectService;
    @Autowired
    private NkTravelscheduleService travelscheduleService;

    Integer cronId = null;

    @Override
    public void setCronId(Integer cronId) {
           this.cronId = cronId;
    }

    @Override
    public void execute() {
        try {
            log.info("start excute SyncNKdataTask");
            Map<String, Object> params = new HashMap<>();

            //异步将离线文件写入数据库
            CompletableFuture.runAsync(() -> {

                aaPersonService.syncData();

                //部门数据
                aaDepartmentService.syncData();
                aaPersonService.syncData();
                //预算指标
                budgetquotaService.syncData();
//                //项目
//                projectService.syncData();
                //呈批
                approvalpaymentreceiptService.syncData();
                approvalpaymentreceiptquotaService.syncData();
//                approvalpaymentreceiptexpensesitemService.syncData();
//                approvalpaymentreceiptsettlementService.syncData();
                //合同
                contractpaymentreceiptService.syncData();
                contractpaymentreceiptquotaService.syncData();
//                contractpaymentreceiptexpensesitemService.syncData();
//                contractpaymentreceiptsettlementService.syncData();
                //直接
                directpaymentreceiptService.syncData();
                directpaymentreceiptquotaService.syncData();
//                directpaymentreceiptexpensesitemService.syncData();
//                directpaymentreceiptsettlementService.syncData();
                //借款
                prepaidpaymentreceiptService.syncData();
                prepaidpaymentreceiptquotaService.syncData();
//                prepaidpaymentreceiptexpensesitemService.syncData();
//                prepaidpaymentreceiptsettlementService.syncData();

                //行程单
                travelscheduleService.syncData();


            }, taskExecutor);

        } catch (Throwable e) {
            log.error("execute process has error(Throwable) : ", e);
        }
    }

    @Override
    public void trigger(String startTime) throws IOException {

    }
}
